/*
 * @Author: 肖思汗
 * @Date: 2025-06-06 09:01:30
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-08-25 18:14:36
 */



import { makeAutoObservable, toJS } from "mobx";
import goodsApi from "src/api/goodsApi";
import initGoodsData from "./initGoodsData";
import initGoodsBaseData from "../Design/designComponent/Designer/initGoodsBaseData";
import goodsBaseApi from "src/api/goodsBaseApi";
import { GoodsBaseData, GoodsData } from "types/type";
import Taro from "@tarojs/taro";
import shoppingCartApi from "src/pages/ShoppingCart/shoppingCartApi";
import { add, get } from "lodash";

class GoodsDetailStore {
    constructor() {
        makeAutoObservable(this, {}, { deep: false });
    }

    // 商品信息
    goodsData: GoodsData = initGoodsData();
    //底板信息
    goodsBaseData: GoodsBaseData = initGoodsBaseData();

    selectedColor = "";
    selectedColorValue: `#${string}` = "#ffffff";

    selectColor(selectedColor: string, selectedColorValue: `#${string}`) {
        debugger;
        console.log("selectColor", selectedColor, selectedColorValue);
        this.selectedColor = selectedColor;
        this.selectedColorValue = selectedColorValue;
    }

    selectedSize = "";
    selectSize(selectedSize: string) {
        this.selectedSize = selectedSize;
    }

    // 获取商品详情
    getGoodsDatails(productId: string = "-1") {
        goodsApi.getProductById(productId).then((goodsData) => {
            this.goodsData = goodsData;
            goodsBaseApi
                .getProductNegativeById(goodsData.productNegativeId)
                .then((goodsBaseData) => {
                    this.goodsBaseData = goodsBaseData;
                    const sizes = goodsBaseData.data.sizes.filter(s => s.enable);
                    sizes[0] && (this.selectedSize = sizes[0].name);
                    this.selectedColor = goodsData.productData.color;
                    this.selectedColorValue = goodsData.productData.colorValue;
                    console.log("this.selectedColor", this.selectedColor);
                });
        });
    }

    joining = false;
    // 加入购物车
    async joinCart() {
        this.joining = true;

        // 颜色对应的衣服
        const thumbs: Array<string> = [];

        if (["直喷", "烫画"].includes(this.goodsBaseData.secondaryCraftName)) {
            if (this.goodsData.productData.colorThumbs) {
                this.goodsData.productData.colorThumbs.map((color) => {
                    console.log("this.selectedColor", this.selectedColor);
                    if (this.selectedColor === color.cn) {
                        if (color.fontThumbImg) {
                            thumbs.push(color.fontThumbImg);
                        }
                        if (color.backThumbImg) {
                            thumbs.push(color.backThumbImg);
                        }
                    }
                });
            }
        } else {
            // 满版印的只有用产品图
            thumbs.push(this.goodsData.productShowImageUrl);
        }

        // 设计数据
        const productDesign: GoodsData["productData"] = {
            ...toJS(this.goodsData.productData),
            thumbs: thumbs, // 商品图片数组
        };

        // 判断单双面
        const hasFront = (get(productDesign.designData, ['front', 'objects', 'length']) || 0) > 0;
        const hasBack = (get(productDesign.designData, ['back', 'objects', 'length']) || 0) > 0;

        if (hasFront && hasBack) {
            productDesign.printArea = 'double';
        } else if (!hasFront && hasBack) {
            productDesign.printArea = 'back';
        } else {
            productDesign.printArea = 'front';
        }

        await shoppingCartApi.add({
            productId: this.goodsData.productId, // 商品id
            productNegativeId: this.goodsBaseData.productNegativeId, // 底板id
            productDesign: productDesign, // 设计数据
            designName: this.goodsData.productName, // 设计者名字
            productShowImageUrl: this.goodsData.productShowImageUrl, // 商品图片
            size: this.selectedSize, // 规格
            color: this.selectedColor, // 颜色
            addToDesign: false,
        });
        Taro.switchTab({
            url: '/pages/ShoppingCart/ShoppingCart'
        });
        this.joining = false;
    }


}

const goodsDetailStore = new GoodsDetailStore();

export default goodsDetailStore;